package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;



public class Proxy_Request extends AxdrType {
    private ByteBuffer dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private ProxyGetRequestList proxyGetRequestList = null;
    private ProxyGetRequestRecord proxyGetRequestRecord = null;
    private ProxySetRequestList proxySetRequestList = null;
    private ProxySetThenGetRequestList proxySetThenGetRequestList = null;
    private ProxyActionRequestList proxyActionRequestList = null;
    private ProxyActionThenGetRequestList proxyActionThenGetRequestList = null;
    private ProxyTransCommandRequest proxyTransCommandRequest = null;

    public Proxy_Request() {
    }

    public void set_dataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setProxyGetRequestList(ProxyGetRequestList proxyGetRequestList) {
        resetChoices();
        this.choice = Choice.PROXY_GET_REQUEST_LIST;
        this.proxyGetRequestList = proxyGetRequestList;
    }

    public void setProxyGetRequestRecord(ProxyGetRequestRecord proxyGetRequestRecord) {
        resetChoices();
        this.choice = Choice.PROXY_GET_REQUEST_RECORD;
        this.proxyGetRequestRecord = proxyGetRequestRecord;
    }

    public void setProxySetRequestList(ProxySetRequestList proxySetRequestList) {
        resetChoices();
        this.choice = Choice.PROXY_SET_REQUEST_LIST;
        this.proxySetRequestList = proxySetRequestList;
    }

    public void setProxySetThenGetRequestList(ProxySetThenGetRequestList proxySetThenGetRequestList) {
        resetChoices();
        this.choice = Choice.PROXY_SET_THEN_GET_REQUEST_LIST;
        this.proxySetThenGetRequestList = proxySetThenGetRequestList;
    }

    public void setProxyActionRequestList(ProxyActionRequestList proxyActionRequestList) {
        resetChoices();
        this.choice = Choice.PROXY_ACTION_REQUEST_LIST;
        this.proxyActionRequestList = proxyActionRequestList;
    }

    public void setProxyActionThenGetRequestList(ProxyActionThenGetRequestList proxyActionThenGetRequestList) {
        resetChoices();
        this.choice = Choice.PROXY_ACTION_THEN_GET_REQUEST_LIST;
        this.proxyActionThenGetRequestList = proxyActionThenGetRequestList;
    }

    public void setProxyTransCommandRequest(ProxyTransCommandRequest proxyTransCommandRequest) {
        resetChoices();
        this.choice = Choice.PROXY_TRANS_COMMAND_REQUEST;
        this.proxyTransCommandRequest = proxyTransCommandRequest;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.PROXY_GET_REQUEST_LIST) {
            this.proxyGetRequestList = new ProxyGetRequestList();
            codeLength += this.proxyGetRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_GET_REQUEST_RECORD) {
            this.proxyGetRequestRecord = new ProxyGetRequestRecord();
            codeLength += this.proxyGetRequestRecord.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_SET_REQUEST_LIST) {
            this.proxySetRequestList = new ProxySetRequestList();
            codeLength += this.proxySetRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_SET_THEN_GET_REQUEST_LIST) {
            this.proxySetThenGetRequestList = new ProxySetThenGetRequestList();
            codeLength += this.proxySetThenGetRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_ACTION_REQUEST_LIST) {
            this.proxyActionRequestList = new ProxyActionRequestList();
            codeLength += this.proxyActionRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_ACTION_THEN_GET_REQUEST_LIST) {
            this.proxyActionThenGetRequestList = new ProxyActionThenGetRequestList();
            codeLength += this.proxyActionThenGetRequestList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.PROXY_TRANS_COMMAND_REQUEST) {
            this.proxyTransCommandRequest = new ProxyTransCommandRequest();
            codeLength += this.proxyTransCommandRequest.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            byte[] dataCodeArray = this.dataCode.array();
            for (int i = dataCodeArray.length - 1; i >= 0; i--) {
                output.write(dataCodeArray[i]);
            }
            return dataCodeArray.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.PROXY_TRANS_COMMAND_REQUEST) {
            if (this.proxyTransCommandRequest != null) {
                codeLength += this.proxyTransCommandRequest.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(7);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxyTransCommandRequest is null");
            }
        } else if (this.choice == Choice.PROXY_ACTION_THEN_GET_REQUEST_LIST) {
            if (this.proxyActionThenGetRequestList != null) {
                codeLength += this.proxyActionThenGetRequestList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(6);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxyActionThenGetRequestList is null");
            }
        } else if (this.choice == Choice.PROXY_ACTION_REQUEST_LIST) {
            if (this.proxyActionRequestList != null) {
                codeLength += this.proxyActionRequestList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(5);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxyActionRequestList is null");
            }
        } else if (this.choice == Choice.PROXY_SET_THEN_GET_REQUEST_LIST) {
            if (this.proxySetThenGetRequestList != null) {
                codeLength += this.proxySetThenGetRequestList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(4);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxySetThenGetRequestList is null");
            }
        } else if (this.choice == Choice.PROXY_SET_REQUEST_LIST) {
            if (this.proxySetRequestList != null) {
                codeLength += this.proxySetRequestList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxySetRequestList is null");
            }
        } else if (this.choice == Choice.PROXY_GET_REQUEST_RECORD) {
            if (this.proxyGetRequestRecord != null) {
                codeLength += this.proxyGetRequestRecord.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxyGetRequestRecord is null");
            }
        } else if (this.choice == Choice.PROXY_GET_REQUEST_LIST) {
            if (this.proxyGetRequestList != null) {
                codeLength += this.proxyGetRequestList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("proxyGetRequestList is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.proxyGetRequestList = null;
        this.proxyGetRequestRecord = null;
        this.proxySetRequestList = null;
        this.proxySetThenGetRequestList = null;
        this.proxyActionRequestList = null;
        this.proxyActionThenGetRequestList = null;
        this.proxyTransCommandRequest = null;
    }

    public String toString() {
        if (this.choice == Choice.PROXY_GET_REQUEST_LIST) {
            if (this.proxyGetRequestList != null) {
                return "choice: {proxyGetRequestList: " + this.proxyGetRequestList + "}";
            } else {
                return "choice is proxyGetRequestList but proxyGetRequestList is null";
            }
        } else if (this.choice == Choice.PROXY_GET_REQUEST_RECORD) {
            if (this.proxyGetRequestRecord != null) {
                return "choice: {proxyGetRequestRecord: " + this.proxyGetRequestRecord + "}";
            } else {
                return "choice is proxyGetRequestRecord but proxyGetRequestRecord is null";
            }
        } else if (this.choice == Choice.PROXY_SET_REQUEST_LIST) {
            if (this.proxySetRequestList != null) {
                return "choice: {proxySetRequestList: " + this.proxySetRequestList + "}";
            } else {
                return "choice is proxySetRequestList but proxySetRequestList is null";
            }
        } else if (this.choice == Choice.PROXY_SET_THEN_GET_REQUEST_LIST) {
            if (this.proxySetThenGetRequestList != null) {
                return "choice: {proxySetThenGetRequestList: " + this.proxySetThenGetRequestList + "}";
            } else {
                return "choice is proxySetThenGetRequestList but proxySetThenGetRequestList is null";
            }
        } else if (this.choice == Choice.PROXY_ACTION_REQUEST_LIST) {
            if (this.proxyActionRequestList != null) {
                return "choice: {proxyActionRequestList: " + this.proxyActionRequestList + "}";
            } else {
                return "choice is proxyActionRequestList but proxyActionRequestList is null";
            }
        } else if (this.choice == Choice.PROXY_ACTION_THEN_GET_REQUEST_LIST) {
            if (this.proxyActionThenGetRequestList != null) {
                return "choice: {proxyActionThenGetRequestList: " + this.proxyActionThenGetRequestList + "}";
            } else {
                return "choice is proxyActionThenGetRequestList but proxyActionThenGetRequestList is null";
            }
        } else if (this.choice == Choice.PROXY_TRANS_COMMAND_REQUEST) {
            if (this.proxyTransCommandRequest != null) {
                return "choice: {proxyTransCommandRequest: " + this.proxyTransCommandRequest + "}";
            } else {
                return "choice is proxyTransCommandRequest but proxyTransCommandRequest is null";
            }
        } else {
            return "unknown";
        }
    }

    enum Choice {
        _ERR_NONE_SELECTED(-1),
        PROXY_GET_REQUEST_LIST(1),
        PROXY_GET_REQUEST_RECORD(2),
        PROXY_SET_REQUEST_LIST(3),
        PROXY_SET_THEN_GET_REQUEST_LIST(4),
        PROXY_ACTION_REQUEST_LIST(5),
        PROXY_ACTION_THEN_GET_REQUEST_LIST(6),
        PROXY_TRANS_COMMAND_REQUEST(7);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



